package global

import (
	"sync"
)

var (
	Ioc  *IocContext
	lock sync.Mutex
)

type (
	IocContext struct {
		Ioc map[string]any
	}
)

func GetIoc() *IocContext {
	if Ioc == nil {
		lock.Lock()
		defer lock.Unlock()
		if Ioc == nil {
			Ioc = &IocContext{
				make(map[string]any),
			}
			return Ioc
		}
	}
	return Ioc
}

// RegisterContext 注册实例
func (i *IocContext) RegisterContext(name string, bean any) {
	if i.Ioc[name] == nil {
		i.Ioc[name] = bean
	}
}

// GetBean 查看实例配置参数
func (i *IocContext) GetBean(name string) any {
	if i.Ioc[name] == nil {
		return nil
	}
	return i.Ioc[name]
}

func (i *IocContext) RemoveBean(name string) {
	if i.Ioc[name] == nil {
		return
	}
	delete(i.Ioc, name)
}
